package com.hjxr.quota.theme.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hjxr.common.core.lang.UUID;
import com.hjxr.quota.theme.domain.QuotaThemeSet;
import com.hjxr.quota.theme.domain.QuotaThemeZone;
import com.hjxr.quota.theme.mapper.QuotaThemeSetMapper;
import com.hjxr.quota.theme.mapper.QuotaThemeZoneMapper;
import com.hjxr.quota.theme.service.IQuotaThemeSetService;
import com.hjxr.quota.theme.service.IQuotaThemeZoneService;
import com.hjxr.quota.util.PageUtil;
import com.hjxr.quota.util.Query;
import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * zoneService业务层处理
 *
 * @author wanxiaochao
 * @ 2020-09-02
 */
@Service
public class QuotaThemeZoneServiceImpl extends ServiceImpl<QuotaThemeZoneMapper, QuotaThemeZone> implements IQuotaThemeZoneService {
    @Resource
    private QuotaThemeZoneMapper quotaThemeZoneMapper;
    @Resource
    private QuotaThemeSetMapper quotaThemeSetMapper;
    @Resource
    private IQuotaThemeSetService quotaThemeSetService;

    /**
     * 查询zone
     *
     * @param id zoneID
     * @return zone
     */
    @Override
    public QuotaThemeZone selectQuotaThemeZoneById(Integer id) {
        return quotaThemeZoneMapper.selectQuotaThemeZoneById(id);
    }

    /**
     * 查询zone列表
     *
     * @param params params
     * @return zone
     */
    @Override
    public PageUtil selectQuotaThemeZoneList(Map<String, Object> params) {

        QueryWrapper<QuotaThemeZone> wrapper = new QueryWrapper<>();
        String zoneTitle = (String) params.get("zoneTitle");
        if (StringUtils.isNotBlank(zoneTitle)) {
            wrapper.like("ZONE_TITLE", zoneTitle);
        }
        IPage<QuotaThemeZone> page = this.page(
                new Query<QuotaThemeZone>().getPage(params),
                wrapper
        );
        return new PageUtil(page);
    }

    @Override
    public PageUtil selectZoneAndSetTreeList(Map<String, Object> params) {

        QueryWrapper<QuotaThemeZone> wrapper = new QueryWrapper<>();
        String zoneTitle = (String) params.get("zoneTitle");
        if (StringUtils.isNotBlank(zoneTitle)) {
            wrapper.like("ZONE_TITLE", zoneTitle);
        }
        IPage<QuotaThemeZone> page = this.page(
                new Query<QuotaThemeZone>().getPage(params),
                wrapper
        );
        List<QuotaThemeZone> records = page.getRecords();
        if (null != records && records.size() > 0) {
            List<Integer> zoneIds = records.stream().map(QuotaThemeZone::getId).collect(Collectors.toList());
            Map<Integer, List<QuotaThemeSet>> setMap = quotaThemeSetService.getThemeSetTreeByZoneIds(zoneIds).stream().collect(Collectors.groupingBy(key -> key.getZoneId()));
            for (QuotaThemeZone record : records) {
                record.setUuid(UUID.randomUUID().toString());
                record.setChildren(setMap.get(record.getId()));
            }
        }
        return new PageUtil(page);
    }

    /**
     * 新增zone
     *
     * @param quotaThemeZone zone
     * @return 结果
     */
    @Override
    public int insertQuotaThemeZone(QuotaThemeZone quotaThemeZone) {
        return quotaThemeZoneMapper.insertQuotaThemeZone(quotaThemeZone);
    }

    /**
     * 修改zone
     *
     * @param quotaThemeZone zone
     * @return 结果
     */
    @Override
    public int updateQuotaThemeZone(QuotaThemeZone quotaThemeZone) {
        return quotaThemeZoneMapper.updateQuotaThemeZone(quotaThemeZone);
    }

    /**
     * 批量删除zone
     *
     * @param ids 需要删除的zoneID
     * @return 结果
     */
    @Override
    public int deleteQuotaThemeZoneByIds(Integer[] ids) {
        QueryWrapper<QuotaThemeSet> wrapper = new QueryWrapper<QuotaThemeSet>().in("ZONE_ID", ids);
        List<QuotaThemeSet> quotaThemeSets = quotaThemeSetMapper.selectList(wrapper);
        if (quotaThemeSets != null && quotaThemeSets.size() > 0) {
            return 0;
        }
        return quotaThemeZoneMapper.deleteQuotaThemeZoneByIds(ids);
    }

    /**
     * 删除quota信息
     *
     * @param id zoneID
     * @return 结果
     */
    @Override
    public int deleteQuotaThemeZoneById(Integer id) {
        return quotaThemeZoneMapper.deleteQuotaThemeZoneById(id);
    }
}
